#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Link all object files for      #
#        the current library          #
#                                     #
#  GDE 03/07/2003 v1.4                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 10/09/2002 v1.3                #
#       add dependancies support      #
#                                     #
#  GDE 07/05/2002 v1.2                #
#       update for SGT 1.1.0          #
#                                     #
#  GDE 25/03/2002 v1.1                #
#       update the version            #
#        and add help sections        #
#                                     #
#  GDE 20/12/2001 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: target file / specific option   #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=1.4
export SCRIPT_SGT_DATE=03/07/2003
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=link_obj
export SCRIPT_SGT_USAGE="Link all object files for the current library"
#-------------------------------------#



#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------



#-------------------------------------#
# Parameters section
#-------------------------------------#
#---------------------
# Parameter 2 -> other options
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ $2 = -d ]
	then
		export OPTION_MAK=DEPENDENCIES=ON
	fi
fi

#---------------------
# Parameter 1 -> target makefile
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					else
						if [ -f ../mak/$1.mak ]
						then
							export TARGET_LIB=$1
						else
							export MSG_ERROR_SCRIPT=1
							export CMD_SCRIPT=Error
						fi
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Link the current library
#---------------------
if [ ! -z $TARGET_LIB ] && [ -z $CMD_SCRIPT ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "
	if [ -z $OPTION_MAK ]
	then
		make -f ../mak/gen.mak TARGET=$TARGET_LIB make_library
	else
		make -f ../mak/gen.mak TARGET=$TARGET_LIB $OPTION_MAK make_library
	fi
	exit
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi

#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [LIBRARY] [-OPTION]"
	echo " "
	echo "  -d  Enable the dependencies for the current library"
	echo " "
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo  "ERROR : $1 or ../mak/$1.mak doesn't exist !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi